package cn.intotw.rdcj.task.spiders.util;

import lombok.SneakyThrows;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
 * Created by Chenxiang
 *
 * @generator: IntelliJ IDEA
 * @description: URI处理工具类
 * @project: rdcj
 * @package: cn.intotw.rdcj.task.spiders.util
 * @date: 2020年07月28日 10时50分
 */
public class URLUtil {
    /**
        because we will make sure that all uri we give in is legal,
        so we handle the exception
     * @author Chenxiang
     * @date 2020/7/28 10:52
     * @param url
     * @return java.net.URI
     **/
    @SneakyThrows(URISyntaxException.class)
    public static URI getUri(String url){
        return new URI(url);
    }

    public static String base64Encode(String url){
        return Base64.getUrlEncoder().encodeToString(url.getBytes(StandardCharsets.UTF_8));
    }

    public static String base64Decode(String base64){
        return new String(Base64.getUrlDecoder().decode(base64),StandardCharsets.UTF_8);
    }
}
